#!/bin/bash
export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/java/latest/bin:/usr/java/latest/jre:/root/bin

. /etc/init.d/functions

#脚本更新应用类型
Deploy_type='tomcat-portal'

#存放实例的路径
Instance_Path="/home/Portal-releases/"

#tomcat程序路径
BASE_Dir='Dcatalina.home=/home/tomcat7'

#生产实例路径
Pr_dir="/home/tomcat7/webapps"

#tomcat启动命令
Start_COMM1="/etc/init.d/tomcat-portal start"

#tomcat关闭命令
Kill_COMM="kill -9 `ps -ef | grep ${BASE_Dir} | grep -v grep | awk '{print $2}'`"

#新实例的webapps路径eg:/home/Portal-releases/webapps20180424
Full_Src_package=$1

#更新日期
Update_date=`date "+%Y-%m-%d"`

#更新日志路径文件
Log_file="/home/rduser/portal_update_script/Deploy_${Update_date}.log"

##################################################以上变量按照实际情况修改#######################################################

#判断参数是否正确,是否有升级包路径与路径的正确性
if (( $# == 1 )) && [[ ${Full_Src_package} =~ ${Instance_Path} ]] ; then
	echo -e "\033[35m-----------------------update begin-----------------------\033[0m"
else
	echo "invalid arguments"
	echo "Usage: sh portal_update.sh Full_Src_package"
	echo "E.g  sh portal_update.sh /home/Portal-releases/webapps20180424"
	exit 1	
fi
 	
#关闭tomcat函数
Kill_tomcat_Service(){
	echo "正在关闭tomcat"
	pid_Count=`ps -ef | grep "${BASE_Dir}" | grep -v grep | wc -l`
	case ${pid_Count} in
		1 )
		while ((${pid_Count}==1)); do
			${Kill_COMM}
			sleep 1s
			pid_Count=`ps -ef | grep ${BASE_Dir} | grep -v grep | wc -l`
		done
		echo `date "+%Y-%m-%d %H:%M:%S"` "${Deploy_type} has been stop" >> ${Log_file}
		echo "${Deploy_type} has been stop"								
			;;
		* )
		echo `date "+%Y-%m-%d %H:%M:%S"` "${Deploy_type} pid_count is not 1" >> ${Log_file}
		echo "${Deploy_type} pid_count is not 1"
		exit 1
	esac	
}

#启动tomcat函数
Start_tomcat_Service(){
	echo "正在启动tomcat"
	pid_Count=`ps -ef | grep ${BASE_Dir} | grep -v grep | wc -l`
	case ${pid_Count} in
		0 )
		while ((${pid_Count}==0)); do
			${Start_COMM1}
			sleep 20s
			pid_Count=`ps -ef | grep ${BASE_Dir} | grep -v grep | wc -l`
		done
		echo `date "+%Y-%m-%d %H:%M:%S"` "${Deploy_type} has been start" >> ${Log_file}
		echo "${Deploy_type} has been start"
		ps -ef | grep ${BASE_Dir} | grep -v grep	
			;;
		*)
		echo `date "+%Y-%m-%d %H:%M:%S"` "${Deploy_type} is already running" >> ${Log_file}
		echo "${Deploy_type} is already running"
		exit 1		
	esac
}

#执行升级函数
Update_Instance(){

	Kill_tomcat_Service

	echo -e "\033[35m----------------------------------------------\033[0m"
	echo "正在删除软连接: mv  ${Pr_dir} /tmp/${Deploy_type}_tmp/webapps.`date "+%Y-%m-%d_%H:%M:%S"`"
	\mv ${Pr_dir} /tmp/${Deploy_type}_tmp/webapps.`date "+%Y-%m-%d_%H:%M:%S"`
	echo "`date "+%Y-%m-%d %H:%M:%S"` 删除软连接: mv  ${Pr_dir} /tmp/${Deploy_type}_tmp/webapps.`date "+%Y-%m-%d_%H:%M:%S"`" >> ${Log_file}
	
	echo -e "\033[35m----------------------------------------------\033[0m"
	echo "正在创建软连接: ln -sv ${Full_Src_package} ${Pr_dir}"
	ln -sv ${Full_Src_package} ${Pr_dir}
	echo "`date "+%Y-%m-%d %H:%M:%S"` 创建软连接: ln -sv ${Full_Src_package} ${Pr_dir}" >> ${Log_file}

	echo -e "\033[35m----------------------------------------------\033[0m"
	Start_tomcat_Service
}


#升级后检查函数
Curl_check(){
	echo -e "\033[35m----------------------------------------------\033[0m"
	echo "执行检查: curl -I http://localhost:8080/iZH-Portal/login"
	curl -I http://localhost:8080/iZH-Portal/login
	echo -e "\033[35m----------------------------------------------\033[0m"
	ls -l /home/tomcat7 | grep --color webapps

}
#####################################主程序########################################

[ -d "/tmp/${Deploy_type}_tmp" ] || mkdir "/tmp/${Deploy_type}_tmp"

#执行升级
Update_Instance


#执行检查
Curl_check
